package org.adultedu.controller;

import org.adultedu.pojo.Student;
import org.adultedu.result.Result;
import org.adultedu.result.ResultEnum;
import org.adultedu.result.ResultUtil;
import org.adultedu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/StudentTable",method = RequestMethod.GET)
    public Result GetStudentTable(){
        List<Student> studentList = studentService.queryAllStudent();
        return ResultUtil.success(studentList);
    }


    @GetMapping(value = "/queryStudent")
    public Result queryStudent(String input,String value){
        int select = Integer.parseInt(value);
        Student student = new Student();
        List list;
        switch (select){
            case 1:{
                student.setName(input);
                break;
            }
            case 2:{
                student.setSfz(input);
                break;
            }
            case 3:{
                student.setTele(input);
                break;
            }
            case 4:{
                student.setEmail(input);
                break;
            }
            default:{
                return ResultUtil.success(studentService.queryUniqueStudent(student),studentService.queryUniqueStudent(student).size());
            }
        }
        list = studentService.queryUniqueStudent(student);
        if(!list.isEmpty()){
            return ResultUtil.success(list,list.size());
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @GetMapping(value = "/getLimitStudent")
    public Result getLimitStudent(String input,String value,Integer currentpage,Integer pagesize){
        int select = Integer.parseInt(value);
        String name,sfz,tele,email;
        name = sfz = tele = email =null;
        List list;
        Integer length;
        currentpage = (currentpage-1)*pagesize;
        switch (select){
            case 1:{
                name = input;
                break;
            }
            case 2:{
                sfz = input;
                break;
            }
            case 3:{
                tele = input;
                break;
            }
            case 4:{
                email = input;
                break;
            }
        }
        Student student = new Student(null,null,name,null,tele,sfz,null,null,null,null,email,null);
        length = studentService.queryUniqueStudent(student).size();
        list = studentService.queryLimitStudent(name,sfz,tele,email,currentpage,pagesize);
        System.out.print(list);
        if(!list.isEmpty()){
            return ResultUtil.success(list,length);
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    public Result updateStudent(@RequestBody Student student){
        if(studentService.updateStudent(student)==1){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @RequestMapping(value = "/insertStudent",method = RequestMethod.POST)
    public Result insertStudent(Student student){
        if (studentService.insertStudent(student)==1){
            return ResultUtil.success();
        }
        else {
            return ResultUtil.error(ResultEnum.INSERT_FAIL.getCode(),ResultEnum.INSERT_FAIL.getMsg());
        }
    }

    @GetMapping(value = "/resetpwd")
    public Result ResetPwd(String sid){
        String pwd;
        String sfz = studentService.getSfzBySid(sid);
        pwd = sfz.substring(10);
        if(studentService.resetStudentAccount(sid,pwd) == 1){
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
        else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @RequestMapping(value = "/diableaccount",method = RequestMethod.POST)
    public Result diableAccount(@RequestBody Map<String,String> map){
        System.out.println(map);
        String sid = map.get("sid");
        String reason = map.get("reason");
        String other = map.get("other");
        if(studentService.disableAccount(sid,reason,other) == 1){
            return ResultUtil.success(ResultEnum.SUCCESS.getCode());
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @GetMapping(value = "/enableaccount")
    public Result diableAccount(String sid){
        if(studentService.enableAccount(sid) == 1){
            return ResultUtil.success(ResultEnum.SUCCESS.getCode());
        }else {
            return ResultUtil.error(ResultEnum.UPDATE_FAIL.getCode(),ResultEnum.UPDATE_FAIL.getMsg());
        }
    }

    @GetMapping(value = "/getSaleStudent")
    public Result getSaleStudent(String cid){
        List list = studentService.getSaleStudent(cid);
        if(!list.isEmpty()){
            return ResultUtil.success(list);
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @GetMapping(value = "/getStudent")
    public Result getStudent(String sid){
        Student s = new Student(sid,null,null,null,null,null,null,null,null,null,null,null);
        List list = studentService.queryUniqueStudent(s);
        if(!list.isEmpty()){
            return ResultUtil.success(list.get(0));
        }else {
            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(),ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @GetMapping(value = "/deletestudent")
    public Result deleteStudent(String sid){
        int i = studentService.deleteStudent(sid);
        if(i == 1){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.DELETE_FAIL.getCode(),ResultEnum.DELETE_FAIL.getMsg());
        }
    }
}
